﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering.PostProcessing;

public class TBSCameraRender : MonoBehaviour
{

	public Camera foreCamera;
	public Camera backCamera;
	public Camera drawingCamera;
	public Canvas canvas;
	public RenderTexture texture;
	public bool renderFore;
	public bool renderBack;
	public bool renderDrawing;
	public RenderTexture CreateTexture()
	{
		texture = new RenderTexture(Screen.width,Screen.height, 0);
		texture.useMipMap = false;
		return texture;
	}
	public RenderTexture CreateTexture(int width,int height)
	{
		texture = new RenderTexture(width, height, 0);
		texture.useMipMap = false;
		return texture;
	}
	public void SetRender(bool renderBack, bool renderFore,bool renderDrawing)
	{
		this.renderFore = renderFore;
		this.renderBack = renderBack;
		this.renderDrawing = renderDrawing;
		if(foreCamera != null)
		{
			foreCamera.enabled = false;
			foreCamera.targetTexture = texture;
			foreCamera.gameObject.SetActive(renderFore);
		}
		if (backCamera != null)
		{
			backCamera.enabled = false;
			backCamera.targetTexture = texture;
			backCamera.gameObject.SetActive(renderBack);
		}
		if (drawingCamera != null)
		{
			drawingCamera.enabled = false;
			drawingCamera.targetTexture = texture;
			drawingCamera.gameObject.SetActive(renderDrawing);
			canvas.gameObject.SetActive(renderDrawing);
		}
			
	}
	public void EnablePostProcess(bool enable)
	{
		var p = foreCamera.GetComponent<PostProcessLayer>();
		p.enabled = enable;
		p = backCamera.GetComponent<PostProcessLayer>();
		p.enabled = enable;
	}
	void Update()
	{
		if (texture == null) return;
		RenderTexture.active = texture;
		GL.Clear(true, true, Color.clear);
		if (renderBack && backCamera != null)
			backCamera.Render();
		if (renderDrawing && drawingCamera != null)
			drawingCamera.Render();
		if (renderFore && foreCamera != null)
			foreCamera.Render();
		RenderTexture.active = null;
	}
	
}
